package com.example.bbs.web.servlet;
/**
 * @auther: zhangxin
 * @Description:
 */

import com.example.bbs.model.Reply;
import com.example.bbs.model.User;
import com.example.bbs.service.ReplyService;
import com.example.bbs.service.impl.ReplyServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "ReplyServlet", value = "/bbs/doReply")
public class ReplyServlet extends HttpServlet {

    ReplyService replyService = new ReplyServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");
        int pid = Integer.parseInt(req.getParameter("pid"));
        String content = req.getParameter("content");
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");
        Reply reply = new Reply(pid,user.getUserId(),content);
        boolean bool = replyService.addReply(reply);
        if(bool){
           resp.sendRedirect("postDetail?pid=" + pid);
        }
    }
}
